// gjw/pages/store/index.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    imgUrls: ['http://www.xgyataigj.net/uploads/180522/4-1P5221A55A24.jpg', 'http://www.xgyataigj.net/uploads/180522/4-1P5221A524234.jpg', 'http://www.xgyataigj.net/uploads/180522/4-1P5221A414923.jpg'],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    circular: true,
    news_list: ['抖宝', '微拍', '地摊', '商城'],
    news_indicatorDots: false,
    news_autoplay: true,
    news_interval: 3000,
    news_duration: 500,
    news_circular: true,
		news_vertical: true,
		gz: 0,
    list: [
      { title: '金丝楠木雕达摩', img: 'http://www.yhpmh.com/images/201702/24/209244441.jpg ', price: 100, see: 200 },
      { title: '金丝楠木雕达摩', img: 'http://www.yhpmh.com/images/201702/24/209244441.jpg ', price: 100, see: 200 },
      { title: '金丝楠木雕达摩', img: 'http://www.yhpmh.com/images/201702/24/209244441.jpg ', price: 100, see: 200 },
      { title: '金丝楠木雕达摩', img: 'http://www.yhpmh.com/images/201702/24/209244441.jpg ', price: 100, see: 200 },

    ],
    tag:'',
    star: [],
    activeIndex:0,
    index:0
    
  },

 
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
		var siteUrl = wx.getStorageSync('siteUrl')
    that.setData({
      info: options,
      siteUrl: siteUrl
    })   
    that.getData() 
		that.once()
		that.gz()    
  },

  change: function (e) {
    var that=this
    var i = e.currentTarget.dataset.index
    this.setData({
      activeIndex: i
    })
    if(i==0){
      that.once()
    }else if (i == 1) {
      that.twice()
    }else if(i==2){
      that.three()
    } else if (i == 3) {
      that.four()
    }
  },

  once:function(e){
    var that=this
    var phone=that.data.info.phone
    app.util.request({
      url: 'entry/wxapp/Loveenterstoredb',
      data: {
        phone: phone
      },
      success:function(res){
        
        
       if(res.data.code==200){
         var arr = res.data.res
         that.setData({
           list: arr
         })
         
       } else if (res.data.code == 500){
         that.setData({
           list: []
         })
       }
       
      }
    })
  },


  // 微拍
  twice:function(e){
    var that = this
    var phone = that.data.info.phone
    app.util.request({
      url: 'entry/wxapp/Loveenterstoreshot',
      data: {
        phone: phone
      },
      success: function (res) {
        
        if (res.data.code == 200) {
          var arr = res.data.res
          for(let i in arr){
            arr[i].img=arr[i].img.split(',')
          }
          that.setData({
            list: arr
          })

        } else if (res.data.code == 500) {
          that.setData({
            list: []
          })
        }
      }
    })
  },

  // 地摊
  three: function (e) {
    var that = this
    var phone = that.data.info.phone
    app.util.request({
      url: 'entry/wxapp/Loveenterstoremarket',
      data: {
        phone: phone
      },
      success: function (res) {
        
        if (res.data.code == 200) {
          var arr = res.data.res
          for (let i in arr) {
            arr[i].img = arr[i].img.split(',')
          }
          that.setData({
            list: arr
          })

        } else if (res.data.code == 500) {
          that.setData({
            list: []
          })
        }
      }
    })
  },
  // 商城
  four: function (e) {
    var that = this
    var phone = that.data.info.phone
    app.util.request({
      url: 'entry/wxapp/Loveenterstorestore',
      data: {
        phone: phone
      },
      success: function (res) {
        
        if (res.data.code == 200) {
          var arr = res.data.res
          for (let i in arr) {
            arr[i].img = arr[i].img.split(',')
          }
          that.setData({
            list: arr
          })

        } else if (res.data.code == 500) {
          that.setData({
            list: []
          })
        }

        
      }
    })
  },

  getData:function(e){
    var that=this
    var phone=that.data.info.phone
    app.util.request({
      url:'entry/wxapp/Lovestoreinfo',
      data:{
        phone:phone
      },
      success:function(res){
        
        that.setData({
          tag: res.data
        })
        var num = that.data.tag.star
        var star=[]
        for(var i=0;i<num;i++){
          star.push('../images/star_on.png')
        }
        that.setData({
          star:star
        })
      }
    })
  },


  // 获取抖宝数据
 


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this
    var activeIndex = that.data.activeIndex
    that.data.page=that.data.page+1
    if (activeIndex==0){
      var phone = that.data.info.phone
      app.util.request({
        url: 'entry/wxapp/Loveenterstoredb',
        data: {
          phone: phone,page:that.data.page
        },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {
            for (var i = 0; i < res.data.res.length; i++) {
              that.data.list.push(res.data.res[i])
            }
            that.setData({
              list: that.data.list
            })
            
          }
         
        }
      })
    } else if (activeIndex==1){
      app.util.request({
        url: 'entry/wxapp/Loveenterstoreshot',
        data: {
          phone: phone, page: that.data.page
        },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {
            var arr=res.data.res
            for (let j in arr) {
              arr[j].img = arr[j].img.split(',')
            }
            for (var i = 0; i < arr.length; i++) {
              that.data.list.push(arr[i])
            }
            that.setData({
              list: that.data.list
            })
            
          }

        }
      })
    }else if(activeIndex==2){
      app.util.request({
        url: 'entry/wxapp/Loveenterstoremarket',
        data: {
          phone: phone, page: that.data.page
        },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {
            var arr=res.data.res
            for (let j in arr) {
              arr[j].img = arr[j].img.split(',')
            }
            for (var i = 0; i < arr.length; i++) {
              that.data.list.push(arr[i])
            }
            that.setData({
              list: that.data.list
            })
            
          }

        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
	// 判断是否关注
	gz: function (e) {
		var that = this
		var gz = that.data.gz
		var openid = wx.getStorageSync('openid')
		var phone = that.data.info.phone

		if (gz == 0) {
			app.util.request({
				url: 'entry/wxapp/Islovestore',
				data: {
					openid: openid, phone: phone
				},
				success: function (res) {

					if (res.data.code == 500) {
						that.setData({
							gz: 0
						})
					} else if (res.data.code == 200)
						that.setData({
							gz: 1
						})
				}
			})
		}
	},
	// 关注(唐补)
	follow: function (e) {
		var that = this
		var gz = that.data.gz
		var openid = wx.getStorageSync('openid')
		var phone = that.data.info.phone

		if (gz == 0) {
			app.util.request({
				url: 'entry/wxapp/Lovestore',
				data: {
					openid: openid, phone: phone
				},
				success: function (res) {

					that.setData({
						gz: 1
					})
					wx.showToast({
						title: '关注成功',
						icon: 'success'
					})
				}
			})
		} else {
			wx.showToast({
				title: '您已经关注该店铺了',
				icon: 'none'
			})
		}
	},
})